﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;

namespace YuuzleComic.Common
{
    /// <summary>
    /// 生成RSS
    /// </summary>
    public class RssUtil
    {
        public static void WriteRSS(RssChannel channel, List<RssItem> items)
        {
            XmlDocument domDoc = new XmlDocument();
            XmlDeclaration nodeDeclar = domDoc.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, "yes");
            domDoc.AppendChild(nodeDeclar);
            //如果rss有样式表文件的话，加上这两句
            //XmlProcessingInstruction nodeStylesheet = domDoc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"rss.css\"");
            //domDoc.AppendChild(nodeStylesheet);
            XmlElement root = domDoc.CreateElement("rss");
            root.SetAttribute("version", "2.0"); //添加属性结点
            domDoc.AppendChild(root);
            //创建Channel信息
            XmlElement chnode = domDoc.CreateElement("channel");
            root.AppendChild(chnode);
            XmlElement element = domDoc.CreateElement("title");
            XmlNode textNode = domDoc.CreateTextNode(channel.Title);    //文本结点
            element.AppendChild(textNode);
            chnode.AppendChild(element);
            element = domDoc.CreateElement("link");
            textNode = domDoc.CreateTextNode(channel.Link);
            element.AppendChild(textNode);
            chnode.AppendChild(element);
            element = domDoc.CreateElement("description"); //引用结点
            XmlNode cDataNode = domDoc.CreateCDataSection(channel.Description);
            element.AppendChild(cDataNode);
            chnode.AppendChild(element);
            //创建项目信息
            foreach (RssItem item in items)
            {
                XmlElement itemNode = domDoc.CreateElement("item");
                element = domDoc.CreateElement("title");
                textNode = domDoc.CreateTextNode(item.Title);    //文本结点
                element.AppendChild(textNode);
                itemNode.AppendChild(element);
                element = domDoc.CreateElement("link");
                textNode = domDoc.CreateTextNode(item.Link);    //文本结点
                element.AppendChild(textNode);
                itemNode.AppendChild(element);
                //element = domDoc.CreateElement("description");
                //textNode = domDoc.CreateTextNode(item.Description);    //文本结点
                //element.AppendChild(textNode);
                //itemNode.AppendChild(element);
                element = domDoc.CreateElement("description");
                XmlNode xlnode = domDoc.CreateCDataSection(item.Description);    //引用结点
                element.AppendChild(textNode);
                itemNode.AppendChild(element);
                element = domDoc.CreateElement("pubDate");
                textNode = domDoc.CreateTextNode(item.PubDate.ToString("yyyy年MM月dd HH:mm"));    //文本结点
                element.AppendChild(xlnode);
                itemNode.AppendChild(element);
                element = domDoc.CreateElement("author");
                textNode = domDoc.CreateTextNode(item.Author);    //文本结点
                element.AppendChild(textNode);
                itemNode.AppendChild(element);
                chnode.AppendChild(itemNode);
            }
            domDoc.Save(new System.Web.UI.Page().Server.MapPath("~/rss.xml"));
            //输出
            //XmlTextWriter objTextWrite = new XmlTextWriter(HttpContext.Current.Response.OutputStream, System.Text.Encoding.UTF8);
            //domDoc.WriteTo(objTextWrite);
            //objTextWrite.Flush();
            //objTextWrite.Close();
            //HttpContext.Current.Response.End();
        }
    }
    /// <summary>
    /// RSS 项目数据
    /// </summary>
    public class RssItem
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
        public DateTime PubDate { get; set; }
        public string Author { get; set; }
        //public string GUID { get; set; }
        //public string Comments { get; set;}
    }
    /// <summary>
    /// RSS 频道数据
    /// </summary>
    public class RssChannel
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
    }
}
